<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-1.11.3.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="lib/bootstrap-3.3.7-dist/css/bootstrap.css" />

</head>
<body>
	<br />
	<form id="ajax_form" method="post"  onsubmit="return false">
		班级：<select id="classId" onchange="selectCourse(this)" name="className">
				<option>--请选择--</option>
		    </select>
		    <br />
		    <br />
		课程：  <select id="courseId" name="courseName">
				<option>--请选择--</option>
		    </select>
		<br />
		<br />
		<input type="button"  onclick="submitForm()" value="选择课程" class="btn btn-primary"/>
	</form>
	
	<script src="js/jquery-1.11.3.js" type="text/javascript" charset="utf-8"></script>
	<script src="lib/layer/layer.js" type="text/javascript" charset="utf-8"></script>
	<script src="lib/mylayer.js" type="text/javascript" charset="utf-8"></script>
	<script src="lib/util.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		$(function(){
			$.post(
				"${pageContext.request.contextPath}/banji?method=selectClass",
				function(jsonArray){
					$(jsonArray).each(function(){
						var html = "<option value='"+this.id+"'>"+this.name+"</option>";
						$("#classId").append(html);
					});
				},
				"json"
			);
		});
		
		function selectCourse(banji){
			var classId = $(banji).val();
			$("#courseId option:gt(0)").remove();
			$.post(
				"${pageContext.request.contextPath}/banji?method=selectCourse",
// 				{"classId" : classId},
				function(jsonArray){
					$(jsonArray).each(function(){
						var html = "<option value='"+this.id+"'>"+this.name+"</option>";
						$("#courseId").append(html);
					});
				},
				"json"
			);
		};
		
		function submitForm(){
			var classId = $("#classId").val();
			var courseId = $("#courseId").val();
			
			$.ajax({
				url:"${pageContext.request.contextPath}/banji?method=chooseCourse",
				type:"post",
				dataType:"json",
				data:{"classId":classId,"courseId":courseId},
				success : function(jsonObj) {
					if(jsonObj.code == util.SUCCESS){
						mylayer.successMsg(jsonObj.msg);
						//当你在iframe页面关闭自身时
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						setTimeout(function(){
							parent.layer.close(index); //再执行关闭 
							// 刷新父页面
							window.parent.location.reload();
						},1000);
					} else {
						mylayer.errorMsg(jsonObj.msg);
					}
				}
			});
		}
	</script>
</body>
</html>